package com.gaohe.web.controller.dcp;

import com.gaohe.common.annotation.DataScope;
import com.gaohe.common.annotation.Log;
import com.gaohe.common.core.controller.BaseController;
import com.gaohe.common.core.domain.AjaxResult;
import com.gaohe.common.core.page.TableDataInfo;
import com.gaohe.common.enums.BusinessType;
import com.gaohe.common.utils.poi.ExcelUtil;
import com.gaohe.dcp.domain.DcpPatient;
import com.gaohe.dcp.domain.DcpScaleScreen;
import com.gaohe.dcp.service.IDcpPatientService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * 病患管理Controller
 *
 * @author ruoyi
 * @date 2025-04-23
 */
@RestController
@RequestMapping("/dcp/patient")
public class DcpPatientController extends BaseController
{
    @Autowired
    private IDcpPatientService dcpPatientService;


    /**
     * 新增量表管理
     */
    @PreAuthorize("@ss.hasPermi('dcp:patient:add')")
    @Log(title = "量表管理", businessType = BusinessType.INSERT)
    @PostMapping("/scaleScreen")
    public AjaxResult addScaleScreen(@RequestBody List<DcpScaleScreen> dcpScaleScreen)
    {
        System.out.println(dcpScaleScreen);
        return toAjax(dcpPatientService.insertDcpPatients(dcpScaleScreen));
    }

    @PreAuthorize("@ss.hasPermi('dcp:patient:edit')")
    @Log(title = "病患医生修改", businessType = BusinessType.UPDATE)
    @GetMapping("/doctor")
    public AjaxResult updateDoctor(DcpPatient dcpPatient)
    {
        return toAjax(dcpPatientService.updateDoctor(dcpPatient));
    }

    /**
     * 查询病患管理列表
     */
    @PreAuthorize("@ss.hasPermi('dcp:patient:list')")
    @GetMapping("/user/list")
    public TableDataInfo list1(DcpPatient dcpPatient)
    {
        startPage();
        List<DcpPatient> list = dcpPatientService.selectDcpPatient(dcpPatient);
        list.forEach(patient -> {
            Date birth = patient.getBirth();
            LocalDate birthDate = birth.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            patient.setAge(Period.between(birthDate, LocalDate.now()).getYears());
        });
        return getDataTable(list);
    }

    @DataScope(userAlias = "p")
    @PreAuthorize("@ss.hasPermi('dcp:patient:list')")
    @GetMapping("/list")
    public TableDataInfo list(DcpPatient dcpPatient)
    {
        startPage();
        List<DcpPatient> list = dcpPatientService.selectDcpPatientList(dcpPatient);
        // 计算年龄
        list.forEach(patient -> {
            Date birth = patient.getBirth();
            LocalDate birthDate = birth.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            patient.setAge(Period.between(birthDate, LocalDate.now()).getYears());
        });
        return getDataTable(list);
    }

    /**
     * 导出病患管理列表
     */
    @PreAuthorize("@ss.hasPermi('dcp:patient:export')")
    @Log(title = "病患管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DcpPatient dcpPatient)
    {
        List<DcpPatient> list = dcpPatientService.selectDcpPatientList(dcpPatient);
        ExcelUtil<DcpPatient> util = new ExcelUtil<DcpPatient>(DcpPatient.class);
        util.exportExcel(response, list, "病患管理数据");
    }

    /**
     * 获取病患管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('dcp:patient:query')")
    @GetMapping(value = "/{patientId}")
    public AjaxResult getInfo(@PathVariable("patientId") Long patientId)
    {
        return success(dcpPatientService.selectDcpPatientByPatientId(patientId));
    }



    /**
     * 修改病患管理
     */
    @PreAuthorize("@ss.hasPermi('dcp:patient:edit')")
    @Log(title = "病患管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DcpPatient dcpPatient)
    {
        return toAjax(dcpPatientService.updateDcpPatient(dcpPatient));
    }

    /**
     * 删除病患管理
     */
    @PreAuthorize("@ss.hasPermi('dcp:patient:remove')")
    @Log(title = "病患管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{patientIds}")
    public AjaxResult remove(@PathVariable Long[] patientIds)
    {
        return toAjax(dcpPatientService.deleteDcpPatientByPatientIds(patientIds));
    }


    //全查
    @PreAuthorize("@ss.hasPermi('dcp:patient:list')")
    @GetMapping("/listPatients")
    public TableDataInfo listPatients()
    {
        List<DcpPatient> list = dcpPatientService.getDcpPatientList();
        return getDataTable(list);
    }
}
